package org.leo.dm.component.engine;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * Created on 2015年11月16日
 * <p>
 * Description 引擎默认实现类
 * <p>
 * Copyright neusoft (c) 2015 .
 * 
 * @author xiao_h
 *
 */
public class DefaultStorage extends Storage {
	Logger log = LoggerFactory.getLogger(this.getClass());

	private final int defaultStorageSize = 20000;

	protected BlockingQueue<Object[]> queue;

	public DefaultStorage() {
		queue = new LinkedBlockingQueue<Object[]>(defaultStorageSize);
	}

	public DefaultStorage(int queueSize) {
		queue = new LinkedBlockingQueue<Object[]>(queueSize);
	}

	public void putStartQueue(String queueName, String extraData) throws Exception {
		if (queueName == null || "".equals(queueName)) {
			queueName = this.getStorageName();
		}
		queue.put(new Object[] { NONBUSINESSDATA, QUEUE, START, queueName, extraData });
	}

	public void putEndQueue() throws Exception {
		queue.put(new Object[] { NONBUSINESSDATA, QUEUE, END });
	}

	public void putStartSeqment(int seqmentNum, String name, String[] columnNames, String extraData) throws Exception {
		queue.put(new Object[] { NONBUSINESSDATA, SEGMENT, START, seqmentNum, name, columnNames, extraData });
	}

	public void putEndSeqment(int seqmentNum) throws Exception {
		queue.put(new Object[] { NONBUSINESSDATA, SEGMENT, END, seqmentNum });
	}

	@Override
	public void putError() throws Exception {
		queue.put(new Object[] { NONBUSINESSDATA, ERROR });
	}

	@Override
	public void putData(Object[] data) throws Exception {
		queue.put(data);
	}

	@Override
	public Object[] getData() throws Exception {
		return queue.take();
	}
}
